package com.wt.struggle.goodbye.main.fragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.*
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.main.activity.AddCommentActivity
import com.wt.struggle.goodbye.main.activity.InvitationDetailsActivity
import com.wt.struggle.goodbye.main.activity.ShareActivity
import com.wt.struggle.goodbye.sq.adapter.PickUserAdapter
import com.wt.struggle.goodbye.user.activity.LoginActivity
import com.wt.struggle.goodbye.user.activity.SiXinOrRecoverActivity
import com.xin.lv.yang.utils.adapter.OnBindRecyclerAdapter
import kotlinx.android.synthetic.main.main_fragment_1.*

class MainFragment1 : ProV4Fragment() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {
            Send.USER_COLLECT_CODE -> {

                swipeRefreshView.isRefreshing = false

                removeLoadDialog()

                if (netInfo.code == Config.SUCCESS) {

                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val arr = gson!!.fromJson<ArrayList<NewsInfo>>(
                            result,
                            object : TypeToken<ArrayList<NewsInfo>>() {}.type
                        )

                        if (arr != null && arr.size != 0) {
                            if (isLoad) {
                                adapter!!.updateData(arr)
                            } else {
                                adapter!!.updateDataClear(arr)
                            }

                            page++

                        } else {

                            if(isVisibleCode){
                                showShortToast(activity!!, "无更多数据")
                            }
                        }
                    }
                } else {
                    if (isVisibleCode) {
                        showShortToast(activity!!, "暂无数据")
                    }

                }
            }

            Send.COLLECT_CODE -> {
                removeLoadDialog()
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (isCollect == 0) {
                        showToastShort(activity!!, "收藏成功")
                    } else {
                        showToastShort(activity!!, "取消收藏成功")
                    }

                    change()

                }
            }

            Send.IS_LIKE_CODE -> {
                removeLoadDialog()
                if (netInfo.code == Config.SUCCESS) {

                    val result = netInfo.dataResult
                    if (isZan == 0) {
                        showToastShort(activity!!, "点赞成功")
                    } else {
                        showToastShort(activity!!, "您已取消点赞")
                    }

                    change()

                }
            }

            Send.ATTENTION_CODE -> {

                if (netInfo.code == Config.SUCCESS) {
                    showToastShort(activity!!, "操作成功")
                    change()

                }
            }

        }


    }


    private fun change() {
        adapter!!.change(clickIndexPosition, clickCode)
    }

    override fun createView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.main_fragment_1, container, false)
    }

    var page = 1
    var isLoad = false

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        initAdapter()

        swipeRefreshView.setOnRefreshListener {
            isLoad = false

            page = 1
            send!!.getCollectList(page, handler!!)
        }

        swipeRefreshView.onLoadMore(attentionRecyclerView) {
            isLoad = true

            send!!.getCollectList(page, handler!!)

        }
    }

    var isVisibleCode = false


    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        isVisibleCode = isVisibleToUser

        get()
    }


    override fun onResume() {
        super.onResume()

    }


    fun get(){

        page = 1
        if (isVisibleCode) {

            if (Share.getToken(activity!!) != "") {

                send!!.getCollectList(page, handler!!)
                showLoadDialog(activity!!, "获取中")

            } else {
                startActivityForResult(Intent(activity!!, LoginActivity::class.java), 7777)
            }

        }
    }


    val list: ArrayList<NewsInfo> = arrayListOf()
    var adapter: PickUserAdapter? = null

    var clickCode = 1
    var clickIndexPosition = -1
    var isCollect = 0
    var isZan = 0

    /**
     * 初始化adapter
     */
    private fun initAdapter() {
        attentionRecyclerView.layoutManager = LinearLayoutManager(activity)
        attentionRecyclerView.addItemDecoration(DividerItemDecoration(activity!!, DividerItemDecoration.VERTICAL))
        adapter = PickUserAdapter(activity!!, list)
        adapter!!.isEnterCode = 4
        attentionRecyclerView.adapter = adapter
        adapter!!.itemClickListener = object : ItemClickListener {
            override fun onItemClick(position: Int) {
                val intent = Intent(activity!!, InvitationDetailsActivity::class.java)
                intent.putExtra("info", list[position])
                startActivity(intent)

            }

            override fun onLongClick(position: Int) {

            }

        }

        adapter!!.onFocusOnClickListener = object : PickUserAdapter.OnFocusOnClickListener {
            override fun onFocusOn(position: Int) {
                clickCode = 5
                this@MainFragment1.clickIndexPosition = position

                send!!.attentionUser(list[position].userExtraInfo!!, handler)
            }

            override fun onCollect(position: Int) {
                val info = list[position]
                isCollect = info.isCollection!!

                clickCode = 1

                this@MainFragment1.clickIndexPosition = position
                send!!.collect(info.id.toString(), 1, handler)
                showLoadDialog(activity!!, "收藏中")
            }

            override fun onZan(position: Int) {
                val info = list[position]

                isZan = info.isLike!!

                clickCode = 2
                this@MainFragment1.clickIndexPosition = position
                send!!.clickZan(info.id!!, 1, handler)
            }

            override fun onDownZan(position: Int) {
                clickCode = 3

                this@MainFragment1.clickIndexPosition = position

                val intent = Intent(activity, SiXinOrRecoverActivity::class.java)
                intent.putExtra("code", 1)
                intent.putExtra("user", list[position].userExtraInfo)
                startActivity(intent)
            }

            override fun onComment(position: Int) {
                clickCode = 4

                this@MainFragment1.clickIndexPosition = position
                val intent = Intent(activity, AddCommentActivity::class.java)
                intent.putExtra("id", list[position].id)
                startActivityForResult(intent, 234)

            }

            override fun onMore(position: Int) {
                this@MainFragment1.clickIndexPosition = position
                openShare(position)
            }

        }

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 234) {
            if (resultCode == Activity.RESULT_OK) {
                change()
            }
        } else if (requestCode == 7777) {

            send!!.getCollectList(page, handler!!)
            showLoadDialog(activity!!, "获取中")
        }
    }


    /**
     * 打开分享弹窗
     */
    fun openShare(adapterPosition: Int) {
        val intent = Intent(activity!!, ShareActivity::class.java)
        val info = list[adapterPosition]
        intent.putExtra("info", info)
        intent.putParcelableArrayListExtra("videoList", info.videoList)
        intent.putParcelableArrayListExtra("voiceList", info.audioList)
        intent.putExtra("code", 1)
        startActivity(intent)

    }
}